package com.xgkj.diyiketang.activity.data;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.xgkj.diyiketang.R;
import com.xgkj.diyiketang.data.AnchorInfo;

import com.xgkj.diyiketang.util.ImageCache;
import com.xgkj.diyiketang.util.ViewUtil;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by XCKJ_Paladowow on 2016/3/15.
 */
public class XYBAnchorAdatper extends BaseAdapter {
    private Context mContext;
    private List<AnchorInfo> mList = new ArrayList<AnchorInfo>();

    public XYBAnchorAdatper(Context context) {
        mContext = context;
    }

    public void updateData(List<AnchorInfo> list) {
        if (mList != null) {
            mList.addAll(list);
        }
        notifyDataSetChanged();
    }

    public void clearData() {
        if (mList != null) {
            mList.clear();
        }
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
//        if(mList != null){
//            return mList.size();
//        }
        return 3;
    }

    @Override
    public Object getItem(int position) {
        if (mList != null) {
            return mList.get(position);
        }
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ChildHolder childHolder = null;
        if (convertView == null || convertView.getTag() == null) {
            childHolder = new ChildHolder();
            convertView = LayoutInflater.from(mContext).inflate(R.layout.xiaoyuanbang_anchor_item, null);
            childHolder.zhuboImg = (ImageView) convertView.findViewById(R.id.xybanchor_icon);
            childHolder.zhuboNick = (TextView) convertView.findViewById(R.id.xybanchor_nick);
            childHolder.zhuboSign = (TextView) convertView.findViewById(R.id.xybanchor_sign);
            childHolder.fansNum = (TextView) convertView.findViewById(R.id.xybanchor_fansnum);
            childHolder.paiming = (TextView) convertView.findViewById(R.id.xybanchor_num);
            childHolder.zhuboCode = (TextView) convertView.findViewById(R.id.xybanchor_code);

            convertView.setTag(childHolder);
        } else {
            childHolder = (ChildHolder) convertView.getTag();
        }
        if (position < mList.size()) {
            AnchorInfo info = mList.get(position);
            if (info != null) {
                String anchorImgUrl = info.getAnchorLogo();
                if (anchorImgUrl != null && anchorImgUrl.length() > 0) {
                    ImageCache.getInstance(mContext).loadBitmapToView(childHolder.zhuboImg, anchorImgUrl, new ViewUtil.Size(ViewUtil.dip2px(mContext, 40), ViewUtil.dip2px(mContext, 40)));
                }
//           childHolder.zhuboNick.setText(info.getNickName());
//            childHolder.zhuboSign.setText(info.getAnchorSign());
                if (info.getNickName() != null) {
                    try {
                        childHolder.zhuboNick.setText(URLDecoder.decode(info.getNickName(), "utf-8"));
                    } catch (UnsupportedEncodingException e) {
                        e.printStackTrace();
                    }
                }
                if (info.getAnchorSign() != null) {
                    try {
                        childHolder.zhuboSign.setText(URLDecoder.decode(info.getAnchorSign(), "utf-8"));
                    } catch (UnsupportedEncodingException e) {
                        e.printStackTrace();
                    }
                }
                childHolder.fansNum.setText("人气："+info.getAnchorVote());
                childHolder.zhuboCode.setText("NO." + info.getAnchorCode());


            }

        } else {
            childHolder.zhuboNick.setText("虚位以待");
        }
        int ph = position + 1;
        if (ph == 1) {
            childHolder.paiming.setBackgroundResource(R.drawable.top1);
        } else if (ph == 2) {
            childHolder.paiming.setBackgroundResource(R.drawable.top2);
        } else if (ph == 3) {
            childHolder.paiming.setBackgroundResource(R.drawable.top3);
        }


        return convertView;
    }

    class ChildHolder {
        ImageView zhuboImg;
        TextView zhuboNick;
        TextView zhuboSign;
        TextView zhuboCode;
        TextView fansNum;
        TextView paiming;

    }
}
